package com.sgb.goods.view.activity.vpm;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityChoiceVipPriceGoodsBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.ChoiceVipPriceGoodsViewModel;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

/**
 * 选择会员价商品
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_CHOICE_VIP_PRICE_GOODS)
public class ChoiceVipPriceGoodsActivity extends BaseActivity<GoodsActivityChoiceVipPriceGoodsBinding, ChoiceVipPriceGoodsViewModel>
        implements SmartRefreshLayoutHelper {

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_choice_vip_price_goods;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ChoiceVipPriceGoodsViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("选择会员价商品");
        mDataBinding.head.right.setText("确定");
        mViewModel.getData(false);
    }

    @Override
    protected void initObserve() {
        mViewModel.listData.observe(this, listData -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, listData);
        });

        mViewModel.dialog_show_text.observe(this, data -> {
            new RoundDialog.Builder(ChoiceVipPriceGoodsActivity.this)
                    .title("温馨提示")
                    .content(data)
                    .confirm("知道了").confirmCallback(v -> mViewModel.getData(false)).show();
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(view -> finish());
        mDataBinding.head.right.setOnClickListener(view -> {
            if (mViewModel.adapter.currentObj == null) {
                MToast.showToast(ChoiceVipPriceGoodsActivity.this, "请选择商品");
                return;
            }
            mViewModel.getGoodsType();
        });
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getData(true);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getData(false);
            }
        });
    }
}
